<template>
	<view class="homeLayout pageBg">
		<custom-nav-bar title="推荐"></custom-nav-bar>
		<view class="banner">
			<swiper autoplay circular indicator-dots indicator-active-color="#fff" indicator-color="rgb(255,255,255,0.5)">
				<swiper-item v-for="item in bannerList" :key="item._id">
					<image :src="item.picurl" mode="aspectFill"></image>
				</swiper-item>
				
			</swiper>
		</view>
		
		<view class="notice">
			<view class="left">
				<uni-icons type="sound-filled" size="20"></uni-icons>
				<text class="text">公告</text>
			</view>
			<view class="center">
				<swiper vertical autoplay interval="1500" duration="300" circular>
					<swiper-item v-for="item in noticeList" :key="item._id">
						<navigator url="/pages/notice/detail">
							{{item.title}}
						</navigator>
					</swiper-item>
				</swiper>
			</view>
			<view class="right">
				<uni-icons type="right" size="16" color="#333"></uni-icons>
			</view>
		</view>
		
		<view class="select">
			<common-title>
				<template #name> 每日推荐</template>
				<template #custom >
					<view class="date">
						<uni-icons type="calendar" size="20"></uni-icons>
						<view class="text">
							<uni-dateformat :date="Date.now()" format="dd"></uni-dateformat>日
						</view>
					</view>
				</template>
			</common-title>
			<view class="content">
				<scroll-view scroll-x >
					<view class="box" @click="preview" v-for="item in recommendList" :key="item._id">
						<image :src="item.smallPicurl" mode="aspectFill"></image>
					</view>
				</scroll-view>
				
			</view>
		</view>
		<view class="theme">
			<common-title>
				<template #name>专题推荐</template>
				<template #custom >
					<navigator class="more" url="">More+</navigator>
				</template>
			</common-title>
			<view class="content">
				<theme-item v-for="item in classifyList" :item="item" :key="item._id"></theme-item>
				<theme-item :isMore="true"></theme-item>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {ref} from 'vue';
	import {apiGetBanner,apiGetRecommend,apiGetNotice,apiGetClassify} from '@/api/api.js'
	import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
	
	// 跳转到预览页面
	const preview = ()=>{
		uni.navigateTo({
			url:"/pages/preview/preview"
		})
	}
	
	// 分享给好友
	onShareAppMessage(()=>{
		return {
			title: "俏猪八壁纸，好看的手机壁纸",
			path: "pages/index/index"
		}
	})
	
	// 分享到朋友圈
	onShareTimeline(()=>{
		return {
			title: "俏猪八壁纸，好看的手机壁纸",
			
			// 不设置 imageUrl 时，默认是 logo 图片
			// imageUrl: ""
		}
	})
	
	// 获取banner 数据
	const bannerList = ref([]);
	const getBannerList = async () => {
		let res = await apiGetBanner();
		bannerList.value = res.data;
	}


	// 获取推荐数据
	const recommendList = ref([]);
	const getDayRecommend = async () => {
		let res = await apiGetRecommend();
		recommendList.value = res.data;
	}
	
	
	// 公告列表
	const noticeList = ref([])
	const getNotices = ()=> {
		apiGetNotice({select:"true"}).then((res) => {
			noticeList.value = res.data;
		})
	}
	
	// 壁纸分类
	const classifyList = ref([]);
	const getClassify = async () => {
		let res = await apiGetClassify({
			select: true
		});
		classifyList.value = res.data;
	}

	getBannerList();
	getDayRecommend();
	getNotices();
	getClassify();
</script>

<style lang="scss">
	@import url("../../common/style/common-style.scss");
	.homeLayout {
		.banner {
			width: 750rpx;
			height: 340rpx;
			padding: 30rpx 0;
			swiper {
				height: 100%;
				width: 100%;
				padding: 0 30rpx;
				swiper-item {
					height: 100%;
					width: 100%;
					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}
		.notice {
			height: 80rpx;
			width: 690rpx;
			padding: 0 30rpx;
			margin: 0 auto;
			border-radius: 40rpx;
			background-color: #f9f9f9;
			line-height: 80rpx;
			display: flex;
			.left {
				width: 140rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				:deep() {
					.uni-icons {
						color: $brand-theme-color !important;
					}
				}
				.text {
					font-size: 28rpx;
					font-weight: 600;
					color: $brand-theme-color;
				}
			}
			.center {
				flex: 1;
				swiper {
					height: 100%;
					swiper-item {
						height: 100%;
						font-size: 30rpx;
						color:#666;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
					}
					
				}
				
			}
			.right {
				width: 40rpx;
			}
		}
		
		.select {
			padding-top: 50rpx;
			.date {
				font-size: 30rpx;
				color: $brand-theme-color;
				display: flex;
				align-items: center;
				justify-content: center;
				:deep() {
					.uni-icons {
						color: $brand-theme-color !important;
					}
				}
			}
			.content {
				width: 720rpx;
				margin-left: 30rpx;
				margin-top: 30rpx;
				scroll-view {
				    /* 禁止换行 */
					white-space: nowrap;
					.box {
						width: 198rpx;
						height: 440rpx;
						display: inline-block;
						margin-right: 15rpx;
						image {
							width: 100%;
							height: 100%;
						}
						
					}
				}
			}
		}
		
		.theme {
			padding-top: 50rpx;
			.more {
				color: #888;
				font-size: 30rpx;
			}
			
			.content {
				padding: 0 30rpx;
				margin-top:30rpx;
				display: grid;
				grid-template-columns: repeat(3, 1fr);
				gap: 15rpx;
			}
		}
	}
	
</style>
